package com.vic.backupx.consumer;

import com.rabbitmq.client.Channel;
import com.vic.backupx.config.BackupExchangeConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 消费者
 */
@Slf4j
@Component
public class LogConsumer {

    @RabbitListener(queues = BackupExchangeConfig.LOG_QUEUE)
    public void receiveConfirmMessage(Message message, Channel channel, CorrelationData correlationData) throws IOException {
        try {
            String msg = new String(message.getBody());
            log.error("日志记录不可路由消息: {}", msg);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.info("消息消费错误");
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }

}
